#include <iostream>

using namespace std;

#define UI unsigned int
typedef struct _sfile
{
	char m_name[256];
	UI m_size;
}sfile;

UI main()
{
	FILE *fp = fopen("./files.txt", "r");
	FILE *fout = fopen("out.exe", "wb");
	bool installer = true;
	UI cnt = 0;
	while (!feof(fp)){
		char sname[256];
		fscanf(fp, "%s", sname);
		printf("combining %s\n", sname);
		FILE *ftmp = fopen(sname, "rb");
		fseek(ftmp, 0, SEEK_END);
		UI size = ftell(ftmp);
		fseek(ftmp, 0, SEEK_SET);
		if (!installer){
			sfile tsfile;
			memset(&tsfile, 0, sizeof(sfile));
			strcpy(tsfile.m_name, sname);
			tsfile.m_size = size;
			fwrite((char *)&tsfile, sizeof(char), sizeof(sfile), fout);
			cnt += tsfile.m_size + sizeof(sfile);
		}
		char buf[1024];
		while (size > 0){
			UI ret = fread(buf, sizeof(char), min(sizeof(buf), size), ftmp);
			ret = fwrite(buf, sizeof(char), ret, fout);
			size -= ret;
		}
		fclose(ftmp);
		installer = false;
	}
	printf("cnt : %u\n", cnt);
	fwrite((char *)&cnt, sizeof(char), sizeof(UI), fout);
	fclose(fp);
	fclose(fout);
	system("pause");
	return 0;
}